/**
 * Add all functionality of chat in word
 * 
 * 1. login
 * 2. logout
 * 3. startWebSocket
 * 4. closeWebSocket
 * 5. sendMsg
 * 6. sendMsgEnter
 */
var wordChat = {};

wordChat.wordChatWS = null;

wordChat.login = function(data, serverName) {
	if ("success" == data.status) {
		if ("true" == $("#loginForm\\:loginFunc").val()) {
			$("#loginDiv").hide();
			$("#loginBtn").hide();
			$("#logoutBtn").show();
			$("#chatDiv").show();
			wordChat.startWebSocket(serverName);
		}
	}
};

wordChat.logout = function(data) {
	if ("success" == data.status) {
		$("#loginDiv").show();
		$("#loginBtn").show();
		$("#logoutBtn").hide();
		$("#chatDiv").hide();
		wordChat.closeWebSocket();
		var chatWindow = $("#loginForm\\:chatMsg");
		chatWindow.val("");
	}
};

wordChat.startWebSocket = function(serverName) {
	var username = $("#loginForm\\:username").val();
	if('WebSocket' in window)
		wordChatWS = new WebSocket("ws://" + serverName + "/wordchat/mywebsocket.wchat?username=" + username);
	else if('MozWebSocket' in window)
		wordChatWS = new MozWebSocket("ws://" + serverName + "/wordchat/mywebsocket.wchat?username=" + username);
	else
		alert("Your browser does not support Web Meeting System!");

	wordChatWS.onmessage = function(evt) {
		var chatWindow = $("#loginForm\\:chatMsg");
		if (null != chatWindow.val() && "" != chatWindow.val()) {
			chatWindow.val(chatWindow.val() + "\n" + evt.data);
		} else {
			chatWindow.val(evt.data);
		}
		chatWindow.scrollTop(chatWindow[0].scrollHeight - chatWindow.height());
	};
};

wordChat.closeWebSocket = function() {
	wordChatWS.close();
};

wordChat.sendMsg = function(data) {
	if ("success" == data.status) {
		var chatArea = $("#loginForm\\:chatArea");
		wordChatWS.send(chatArea.val());
		chatArea.val("");
		chatArea.innerHeight(60);
	}
};

wordChat.sendMsgEnter = function(event) {
	var e = event || window.event;
	if (e.ctrlKey && 13 === e.keyCode) {
		var chatArea = $("#loginForm\\:chatArea");
		wordChatWS.send(chatArea.val());
		chatArea.val("");
		chatArea.innerHeight(60);;
	}
};

/* For jsp page
$(document).ready(function() {
	$("#connectBtn").show();
	$("#disconnectBtn").hide();
});
var wordChat = {};
wordChat.wordChatWS = null;
wordChat.startWebSocket = function(server, chatId, msgId, usernameTxtId) {
	var usernameTxt = $("#" + usernameTxtId).val();
	if (null == usernameTxt || "" === usernameTxt) {
		var vNum = 0;
		vNum = Math.random();
		vNum = Math.round(vNum * 1000);
		usernameTxt = "Visitor" + vNum;
	}
	if('WebSocket' in window)
		wordChatWS = new WebSocket("ws://" + server + "/wordchat/mywebsocket.wchat?username=" + usernameTxt);
	else if('MozWebSocket' in window)
		wordChatWS = new MozWebSocket("ws://" + server + "/wordchat/mywebsocket.wchat?username=" + usernameTxt);
	else
		alert("Your browser does not support Web Meeting System!");
	wordChatWS.onmessage = function(evt) {
		var chatWindow = $("#" + chatId);
		chatWindow.html(evt.data + "\n\n" + chatWindow.html());
	};
	wordChatWS.onclose = function(evt) {
		var msgWindow = $("#" + msgId);
		msgWindow.html("Disconnect!");
		$("#connectBtn").show();
		$("#disconnectBtn").hide();
		$("#usernameLbl").show();
		$("#usernameTxt").show();
	};
	wordChatWS.onopen = function(evt) {
		var msgWindow = $("#" + msgId);
		msgWindow.html("Welcome you, " + $("#" + usernameTxtId).val() + "!");
		$("#connectBtn").hide();
		$("#disconnectBtn").show();
		$("#usernameLbl").hide();
		$("#usernameTxt").hide();
		$("#usernameTxt").val("");
	};
};
wordChat.sendMsg = function(sendId) {
	wordChatWS.send($("#" + sendId).val());
	$("#" + sendId).val("");
};
wordChat.close = function() {
	wordChatWS.close();
};
wordChat.clearMsg = function(chatId) {
	var chatWindow = $("#" + chatId);
	chatWindow.html("");
};
wordChat.enterPress = function(event) {
	var e = event || window.event;
	if (e.ctrlKey && 13 === e.keyCode) {
		wordChatWS.send($("#writeMsg").val());
		$("#writeMsg").val("");
	}
};
wordChat.login = function(event, server, chatId, msgId, usernameTxtId) {
	var e = event || window.event;
	if (13 === e.keyCode) {
		wordChat.startWebSocket(server, chatId, msgId, usernameTxtId);
	}
};
*/